package cc.rengu.redp.bizimpl.csmc.controller;

import cc.rengu.redp.bizimpl.csmc.domain.CAcctEntry;
import cc.rengu.redp.bizimpl.csmc.service.ICAcctEntryService;
import cc.rengu.redp.bizimpl.csmc.service.ICSubjectInfoService;
import cc.rengu.redp.bizimpl.csmc.vo.CAcctEntryVO;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lizk
 */
@RestController
@Slf4j
@RequestMapping("/admin/csmc/acctEntry")
public class CAcctEntryController extends BaseController {

    @Autowired
    private ICAcctEntryService acctEntryService;
    @Autowired
    private ICSubjectInfoService subjectInfoService;

    @GetMapping
    public Map<String, Object> findByPage(QueryRequest queryRequest, CAcctEntry acctEntry) {
        return acctEntryService.findByPage(queryRequest, acctEntry);
    }

    @Log("新增/修改/停用/启用 清分规则")
    @PostMapping
//    @RequiresPermissions("brand:add")
    public void createAcctEntry(@RequestBody CAcctEntryVO cAcctEntryVO) throws RedpException {

        try {
            this.acctEntryService.createAcctEntry(cAcctEntryVO.getAcctEntryList(), cAcctEntryVO.getOptFlag());
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }

    }

    /**
     * 查与acctEntryId相等的数据
     * @param id
     * @param optFlag
     * @param acctEntryId
     * @param acctEntryDesc
     * @return
     * @throws RedpException
     */
    @GetMapping("searchAcctEntryData")
    public Map<String, Object> searchAcctEntryData(@RequestParam("id") String id,
                                                @RequestParam("optFlag") String optFlag,
                                                @RequestParam("acctEntryId") String acctEntryId,
                                                @RequestParam("acctEntryDesc") String acctEntryDesc) throws RedpException {
        return acctEntryService.searchAcctEntryData(id, optFlag, acctEntryId, acctEntryDesc);
    }


    /**
     * 会计分录下拉数据
     *
     * @param q 条件
     * @return
     */
    @GetMapping("/select2")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "参数", required = true, dataType = "String")
    })
    public Map<String, Object> tagsData(String q, QueryRequest request) {

        return acctEntryService.findAllAcctEntryIdByOprStatus(BusinessContainer.impStatus_01, q, request);

    }

    @GetMapping("/acctEntryDialog")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "参数", required = true, dataType = "String")
    })
    public Map<String, Object> selectSubjectInfo(String q, QueryRequest request) {
        String flag = "acctEntryDialog";
        return subjectInfoService.tagsData(q, flag, request);
    }


}
